//
//  M1PersonalDataVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/13.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1PersonalDataVC.h"
#import "M1EditDetailsCell.h"
#import "M1EditInformationTextFieldVC.h"
#import "M1EditInformationVC.h"
#import "M1IntroductionMeCell.h"

@interface M1PersonalDataVC ()

@property (nonatomic, strong) NSArray *m1_titleArr;

@property (nonatomic, strong) NSMutableArray *m1_dataArr;

@property (nonatomic, strong) NSMutableArray *m1_highArr;

@end

@implementation M1PersonalDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"编辑资料";
    
    [self m1_createRightBarButtonItem:@"保存" titleColor:m1_color(0x000000) action:@selector(m1_rightAction)];
    
    self.m1_highArr = [NSMutableArray arrayWithCapacity:0];
    
    [self.m1_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(m1_NAV_HEIGHT, 0, 0, 0));
    }];
    
//    [self.m1_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, STATUS_HEIGHT + 44 + 65, 0));
//    }];
    
    [self.m1_tableView registerNib:[UINib nibWithNibName:@"M1EditDetailsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:m1_tableViewCellIdentifier];
    
    [self.m1_tableView registerClass:[M1IntroductionMeCell class] forCellReuseIdentifier:@"M1IntroductionMeCellIdentifier"];
    
    
    for (int i = 140; i < 201; i++) {
        [self.m1_highArr addObject:[NSString stringWithFormat:@"%d", i]];
    }
}

- (void)m1_rightAction{
    
    NSDictionary *params = @{@"native_place":self.m1_dataArr[7],
                             @"job":self.m1_dataArr[6],
                             @"abode":self.m1_dataArr[5],
                             @"annual_income":self.m1_dataArr[8],
                             @"about_me":self.m1_dataArr[10],
                             @"high":self.m1_dataArr[9],
                             @"name":self.m1_dataArr[0],
                             };
    WEAKSELF
    [M1HttpNet requestWithMethod:m1_update_info_url params:params success:^(NSDictionary *resDic, NSInteger code, NSString *message) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
        [M1User m1_uploadInfo:^(M1User *user) {
        }];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        
    } showAni:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.m1_titleArr[indexPath.row] isEqualToString:@"年收入"]) {
        WEAKSELF
        [BRStringPickerView showStringPickerWithTitle:self.m1_titleArr[indexPath.row] dataSource:@[@"10w以下", @"10w~20w", @"20w~30w", @"30w~50w", @"50w~100w", @"100w以上"] defaultSelValue:self.m1_dataArr[indexPath.row] resultBlock:^(id selectValue) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:selectValue];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"所在地"] || [self.m1_titleArr[indexPath.row] isEqualToString:@"来自"]){
        WEAKSELF;
        [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeCity defaultSelected:nil isAutoSelect:NO themeColor:nil resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"%@-%@",province.name,city.name]];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        } cancelBlock:^{
            
        }];
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"昵称"] ||[self.m1_titleArr[indexPath.row] isEqualToString:@"微信号"] || [self.m1_titleArr[indexPath.row] isEqualToString:@"职位"]) {
        M1EditInformationTextFieldVC *textfieldVC = [[M1EditInformationTextFieldVC alloc] init];
        textfieldVC.titleStr = self.m1_titleArr[indexPath.row];
        textfieldVC.textStr = self.m1_dataArr[indexPath.row];
        [self.navigationController pushViewController:textfieldVC animated:YES];
        WEAKSELF
        textfieldVC.returnText = ^(NSString *text) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:text];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        };
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"自我介绍"]){
        M1EditInformationVC *editVC = [[M1EditInformationVC alloc] init];
        editVC.titleStr = self.m1_titleArr[indexPath.row];
        editVC.textStr = self.m1_dataArr[indexPath.row];
        [self.navigationController pushViewController:editVC animated:YES];
        WEAKSELF
        editVC.returnText = ^(NSString *text) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:text];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        };
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"出生日期"]){
        [M1Alert m1_alertWithTitle:nil message:@"为保证用户资料的真实性，出生日期将自动从本人身份证号码读取，无法修改"];
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"星座"]){
        [M1Alert m1_alertWithTitle:nil message:@"根据生日自动匹配星座哦"];
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"身高"]){
        WEAKSELF
        
        [BRStringPickerView showStringPickerWithTitle:self.m1_titleArr[indexPath.row] dataSource:self.m1_highArr defaultSelValue:self.m1_dataArr[indexPath.row] resultBlock:^(id selectValue) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:selectValue];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }else if ([self.m1_titleArr[indexPath.row] isEqualToString:@"期望结婚时间"]){
        WEAKSELF
        
        [BRStringPickerView showStringPickerWithTitle:self.m1_titleArr[indexPath.row] dataSource:@[@"半年内", @"一年内", @"两年内"] defaultSelValue:self.m1_dataArr[indexPath.row] resultBlock:^(id selectValue) {
            [weakSelf.m1_dataArr replaceObjectAtIndex:indexPath.row withObject:selectValue];
            [weakSelf.m1_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.m1_titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat heightForRow = 54;
    
    if ([self.m1_titleArr[indexPath.row] isEqualToString:@"自我介绍"]) {
        CGRect rect = [self.m1_dataArr[indexPath.row] boundingRectWithSize:CGSizeMake(iPhone5?140:200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : m1_font(15)} context:nil];

        heightForRow = rect.size.height + 40;
    }
    
    return heightForRow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.m1_titleArr[indexPath.row] isEqualToString:@"自我介绍"]) {
        M1IntroductionMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"M1IntroductionMeCellIdentifier"];
        
        cell.m1_title.text = self.m1_titleArr[indexPath.row];
        
        cell.m1_content.text = self.m1_dataArr[indexPath.row];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    M1EditDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:m1_tableViewCellIdentifier];
    
    if (indexPath.row == 1) {
        cell.m1_across.hidden = YES;
    }else{
        cell.m1_across.hidden = NO;
    }
        
    [cell setTitle:self.m1_titleArr[indexPath.row] content:self.m1_dataArr[indexPath.row]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


- (NSMutableArray *)m1_dataArr{
    if (!_m1_dataArr) {
        _m1_dataArr = [[NSMutableArray alloc] initWithObjects:
                       [M1User user].basic.name,
                       [M1User user].basic.uid,
                       [M1User user].basic.wechat,
                       [M1User user].basic.birthday,
                       [M1User user].basic.constellation,
                       [M1User user].basic.abode,
                       [M1User user].basic.job,
                       [M1User user].basic.native_place,
                       [M1User user].basic.annual_income,
                       [M1User user].basic.high,
                       [M1User user].basic.about_me, nil];
    }
    return _m1_dataArr;
}

- (NSArray *)m1_titleArr{
    if (!_m1_titleArr) {
        _m1_titleArr = [[NSArray alloc] initWithObjects:
                        @"昵称",
                        @"UID",
                        @"微信号",
                        @"出生日期",
                        @"星座",
                        @"所在地",
                        @"职位",
                        @"来自",
                        @"年收入",
                        @"身高",
                        @"自我介绍", nil];
    }
    return _m1_titleArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
